package com.mt.api.core.threadpool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @Author: plover
 * @Date: 2020/07/09/3:13 下午
 * @Description:
 */
@Slf4j
public class ThreadPoolUtils {

    public static ExecutorService getThreadPool(String threadName) {
        ExecutorService pool = initExecutorService(10, threadName);
        return pool;
    }

    public static ExecutorService getThreadPool(String threadName, int poolSize) {
        ExecutorService pool = initExecutorService(poolSize, threadName);
        return pool;
    }

    private static ExecutorService initExecutorService(int poolSize, String threadName) {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat(threadName).build();
        log.info("获取线程池, 线程名称:{}", threadName);

        LinkedBlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>(10240);
        //Common Thread Pool
        ExecutorService pool = new ThreadPoolExecutor(10, 10,
                0L, TimeUnit.MILLISECONDS,
                blockingQueue, namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        return pool;
    }


    // public static void main(String[] args) {
    //     ThreadPoolUtils.getThreadPool("test-thread-name");
    // }

}
